package com.lcz.googleplay.holder;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.lcz.googleplay.activity.BaseApplication;
import com.lcz.googleplay.config.BaseProtocol;
import com.lcz.googleplay.utils.UIUtils;
import com.lcz.googleplay.utils.ViewUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * 首页轮询图片
 * Created by hp on 2016/10/29.
 */

public class HomePictureHolder extends BaseHolder<List<String>> {
    private List<String> data;
    private ViewPager viewPager;

    @Override
    public View init() {
//        FrameLayout frameLayout = new FrameLayout(UIUtils.getActivity());
//        frameLayout.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        viewPager = new ViewPager(UIUtils.getActivity());
        viewPager.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200));
//        frameLayout.addView(viewPager);
        return viewPager;
    }

    @Override
    public void refreshView(List<String> data) {
        this.data = data;
        viewPager.setAdapter(new PictureAdapter());
        /*设置当前位置，可以让其左右滑动*/
        viewPager.setCurrentItem(1000);
        final AutoRunTask autoRunTask = new AutoRunTask();
        autoRunTask.start();
        viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN://按下
                        autoRunTask.stop();
                        break;
                    case MotionEvent.ACTION_CANCEL://时间取消
                    case MotionEvent.ACTION_UP://抬起
                        autoRunTask.start();
                        break;
                }
                return false;
            }
        });
    }

    /*图片自动轮询*/
    class AutoRunTask implements Runnable {
        private boolean flag;

        @Override
        public void run() {
            if (flag) {
                //先取消当前执行的任务
                BaseApplication.getHandler().removeCallbacks(this);
                //获取当前位置，进行+1改变
                int currentItem = viewPager.getCurrentItem();
                currentItem++;
                viewPager.setCurrentItem(currentItem);
                //延迟时间执行
                BaseApplication.getHandler().postDelayed(this, 2000);
            }
        }

        public void start() {
            if (!flag) {
                //先取消当前执行的任务
                BaseApplication.getHandler().removeCallbacks(this);
                flag = true;
                BaseApplication.getHandler().postDelayed(this, 2000);
            }
        }

        public void stop() {
            if (flag) {
                flag = false;
                //移除当前执行的任务
                BaseApplication.getHandler().removeCallbacks(this);
            }
        }
    }

    class PictureAdapter extends PagerAdapter {
        LinkedList<ImageView> imageViewLinkedList = new LinkedList<>();

        /*可以滑动的次数*/
        @Override
        public int getCount() {
            return Integer.MAX_VALUE;//data.size();
        }

        /*判断加载的对象和返回的对象关系*/
        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ImageView imageView = (ImageView) object;
            imageViewLinkedList.add(imageView);//移除前进行存储
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            /*当前返回的数量是Integer.MAX_VALUE，防止越界*/
            int index = position % data.size();
            ImageView imageView;
            //每次滑动都new ImageView，所以需要将imageview保存复用
            if (imageViewLinkedList.size() > 0) {
                //复用
                imageView = imageViewLinkedList.remove(0);
            } else {
                imageView = new ImageView(UIUtils.getActivity());
            }
            ViewUtils.getImageLoader().displayImage(BaseProtocol.IMAGE_URL + data.get(index), imageView, ViewUtils.getOptions());
            container.addView(imageView);
            return imageView;
        }
    }
}
